<template>
  <div class="app-container">
    <el-main>
      <div class="search">
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="form-inline" size="mini">
          <search-form-box>
            <template slot="showSearch">
              <el-form-item prop="name">
                <label slot="label" v-html="'姓&emsp;&emsp;名:'"/>
                <el-input v-model.trim="searchForm.name" clearable placeholder="请输入姓名"/>
              </el-form-item>
              <el-form-item label="当前状态:" prop="flow_status">
                <el-select v-model="searchForm.flow_status" clearable>
                  <el-option value="0" label="保存">保存</el-option>
                  <el-option value="2" label="退回">退回</el-option>
                  <el-option value="3" label="流程中">流程中</el-option>
                  <el-option value="4" label="通过">通过</el-option>
                  <el-option value="5" label="删除">删除</el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="员工类型:" prop="staff_type_id">
                <el-select v-model="searchForm.staff_type_id" clearable>
                  <el-option v-for="staff in staffTypeList" :key="staff.id" :value="staff.id" :label="staff.name">
                    {{ staff.name }}
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="拟离职日期:">
                <el-col :span="11">
                  <el-form-item prop="plan_leave_date_min">
                    <el-date-picker
                      v-model="searchForm.plan_leave_date_min"
                      clearable
                      type="date"
                      placeholder="选择拟离职开始日期"
                      format="yyyy年MM月dd日"
                      value-format="yyyy-MM-dd"
                    />
                  </el-form-item>
                </el-col>
                <el-col class="txt-center" :span="2">~</el-col>
                <el-col :span="11">
                  <el-form-item prop="plan_leave_date_max">
                    <el-date-picker
                      v-model="searchForm.plan_leave_date_max"
                      clearable
                      type="date"
                      placeholder="选择拟离职结束日期"
                      format="yyyy年MM月dd日"
                      value-format="yyyy-MM-dd"
                    />
                  </el-form-item>
                </el-col>
              </el-form-item>
            </template>
            <template slot="hideSearch">
              <el-form-item label="合同截至日期:">
                <el-col :span="11">
                  <el-form-item prop="contract_end_at_min">
                    <el-date-picker
                      v-model="searchForm.contract_end_at_min"
                      clearable
                      type="date"
                      placeholder="选择合同截止开始日期"
                      format="yyyy年MM月dd日"
                      value-format="yyyy-MM-dd"
                    />
                  </el-form-item>
                </el-col>
                <el-col class="txt-center" :span="2">~</el-col>
                <el-col :span="11">
                  <el-form-item prop="contract_end_at_max">
                    <el-date-picker
                      v-model="searchForm.contract_end_at_max"
                      clearable
                      type="date"
                      placeholder="选择合同截止结束日期"
                      format="yyyy年MM月dd日"
                      value-format="yyyy-MM-dd"
                    />
                  </el-form-item>
                </el-col>
              </el-form-item>
            </template>

            <el-form-item slot="searchBtn">
              <el-button type="primary" plain @click="searchSubmit('searchForm')">搜索</el-button>
              <el-button type="info" plain @click="searchReset">重置</el-button>
            </el-form-item>
          </search-form-box>
        </el-form>

      </div>

      <!-- <el-row :gutter="20" type="flex" align="center">
        <el-col>
          <el-button v-permission="'user_add'" type="primary" size="small" @click.native="showAdd()">离职申请</el-button>
        </el-col>
      </el-row>
      <br> -->

      <el-table
        size="mini"
        ref="multipleTable"
        v-loading="tabLoading"
        stripe
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        border
        :default-sort="{prop: 'date', order: 'descending'}"
        highlight-current-row
        row-key="id"
      >
        <el-table-column type="index" label="序号" align="center" width="50">
          <template slot-scope="scope">
            <span>{{ (listParams.page - 1) * listParams.page_size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="flow_status" label="流程状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.flow_status == 0" size="mini" type="info">草稿</el-tag>
            <el-tag v-else-if="scope.row.flow_status == 2" size="mini" type="warning">退回</el-tag>
            <el-tag v-else-if="scope.row.flow_status == 3" size="mini">{{ scope.row.flow_current_node_name }}</el-tag>
            <el-tag v-else-if="scope.row.flow_status == 4" size="mini" type="success">通过</el-tag>
            <el-tag v-else-if="scope.row.flow_status == 5" size="mini" type="danger">删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="姓名" align="center" show-overflow-tooltip/>
        <el-table-column prop="dep" label="部门" align="center"/>
        <el-table-column prop="job" label="职位" align="center"/>
        <el-table-column prop="contract_end_at" label="合同截止日期" align="center" show-overflow-tooltip/>
        <el-table-column prop="plan_leave_date" label="拟离职日期" align="center" show-overflow-tooltip/>
        <el-table-column prop="staff_type" label="员工类型" align="center" show-overflow-tooltip/>
        <el-table-column label="操作" align="center" width="300px" fixed="right">
          <template slot-scope="scope">
            <div class="operation">
              <el-button-group>
                <el-button type="primary" icon="el-icon-zoom-in" size="mini"
                           @click.native="viewDetails(scope.row.id)"
                >查看
                </el-button>
                <el-upload size="mini" v-if="scope.row.flow_status == 4" :action="upFileUrl"
                           style="float: left;" :show-file-list="false" :headers="uploadHeaders"
                           :on-success="function (res, file) { return handleSuccess(res, file, scope.row.id)}"
                >
                  <el-button size="mini" icon="el-icon-upload" type="danger">上传交接单</el-button>
                </el-upload>
              </el-button-group>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页数据 -->
      <el-pagination
        :current-page.sync="current"
        :page-sizes="[10, 20, 50, 100, 200, 500]"
        :page-size="listParams.page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-main>

  </div>
</template>

<script>
import { list, uploadHandoverSheet } from '@/api/personnel/leave_office'
import {
  formatDateByFmt, generateUUID
} from '@/utils/util.js'
import {
  getToken
} from '@/utils/auth'
import SearchFormBox from '@/components/SearchFormBox'
import { list_all3rd_bycode } from '@/api/business/setting/dict.js'
import { Message } from 'element-ui'
import {
  upFileUrl
} from '@/api/others'

export default {
  name: 'LeaveOfficeList',
  components: {
    SearchFormBox
  },
  data() {
    return {
      staffTypeList: [],
      upFileUrl,
      // search form
      searchForm: {
        name: null,
        flow_status: null,
        staff_type_id: null,
        plan_leave_date_min: null,
        plan_leave_date_max: null,
        contract_end_at_min: null,
        contract_end_at_max: null
      },
      // page option
      total: null, // 总页数
      current: 1, // 当前页

      // list table
      tableData: [], // 列表数据
      listParams: {
        page: 1,
        page_size: 10
      }, // list参数
      tabLoading: true,

      uploadHeaders: {}
    }
  },
  created() {
    this.getList()
    this.getStaffTypeList()
    if (getToken()) {
      this.uploadHeaders['Authorization'] = getToken()
    }
  },
  methods: {
    getStaffTypeList() {
      return list_all3rd_bycode({
        code: 'ryxg-yglb'
      }).then(res => {
        this.staffTypeList = res.data
      })
    },
    // 重置搜索表单
    searchReset() {
      this.$refs['searchForm'].resetFields()
      this.listParams = Object.assign(this.listParams, this.searchForm)
      this.getList()
    },
    // 搜索
    searchSubmit() {
      this.listParams.page = 1
      this.current = 1
      this.listParams = Object.assign(this.listParams, this.searchForm)
      this.getList()
    },

    handleDateRange(data, key, startKey, endKey) {
      if (data[key]) {
        data[startKey] = formatDateByFmt(data[key][0], 'yyyy-MM-dd')
        data[endKey] = formatDateByFmt(data[key][1], 'yyyy-MM-dd')
      }
      delete data[key]
    },
    // 跳转新增
    showAdd() {
      this.$router.push({ name: 'leave_office' })
    },
    viewDetails(id) {
      // const uuid = generateUUID()
      // this.$store.commit('keep/PUT_PAGE_PARAMS', { uuid, data: { id: id } })
      this.$router.push({ name: 'leave_office', params: { id } })
    },
    uploadHandoverSheet() {
      alert('上传离职交接单')
    },
    // 获得table列表
    getList() {
      this.tabLoading = true
      return list(this.listParams)
        .then(res => {
          const {
            data: {
              records,
              total
            }
          } = res
          this.tableData = records.map(item => {
            if (item.role_names) {
              item.role_names = item.role_names.split(',')
            } else {
              item.role_names = []
            }

            if (item.department_list) {
              item.departmen_list_name = item.department_list.map((item, index) => {
                return item.name + (item.job ? ('(' + item.job + ')') : '')
              }).join('、')
            }
            return item
          })
          this.total = total
          this.tabLoading = false
        })
        .catch(() => {
          this.tabLoading = false
        })
    },
    // 分页 -pageSize改变
    handleSizeChange(val) {
      this.listParams.page_size = val
      this.getList()
    },
    // 分页 -pageNum改变
    handleCurrentChange(val) {
      this.listParams.page = val
      this.getList()
    },
    // 带状态表格
    tableRowClassName({
      row
    }) {
      if (row.is_joined === 0) {
        return 'stop-row'
      }
      return ''
    },
    // 上传成功回调方法
    handleSuccess(res, file, id) {
      const _this = this
      if (res.code === 200) {
        _this.uploadAttachment(res.data, id)
        _this.$forceUpdate()
        // _this.$emit('upload-success', res.data)
      } else {
        Message.error({
          message: res.message,
          duration: 5000
        })
      }
    },
    uploadAttachment(url, id) {
      return uploadHandoverSheet(url, id)
        .then(res => {
          Message({
            message: res.data,
            type: 'success',
            duration: 1000
          })
        })
    }
  }
}
</script>

<style scoped>
.txt-center {
  text-align: center;
}
</style>
